#!/usr/bin/ruby
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
require 'libglade2'
require 'yaml'

class GaostarsGlade
  include GetText

  attr :glade, :config_file
  
  def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
    bindtextdomain(domain, localedir, nil, "UTF-8")
    @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
    @config_file = "../config/config.yml"
    @config = YAML.load_file(@config_file)
    put_initial_values_in_entries
  end
  
  def gtk_main_quit(widget)
    Gtk.main_quit
  end  

  def put_initial_values_in_entries
    if (File.directory?("../"+@config["input"]["path"]))
      @glade.get_widget('filebutton_input_dir').set_current_folder("../"+@config["input"]["path"])
    end
    if (File.directory?("../"+@config["output"]["path"]))
      @glade.get_widget('filebutton_output_dir').set_current_folder("../"+@config["output"]["path"])
    end
    puts '../'+@config["input"]["lpoint"]
    if (File.exist?('../'+@config["input"]["lpoint"]))
      @glade.get_widget('filebutton_eph').filename = '../'+@config["input"]["lpoint"]
    end
    @glade.get_widget('entry_lpoint_date').text = @config["input"]["lpoint_date"]
    @glade.get_widget('entry_curve').text = @config["plot"]["curve"]
    @glade.get_widget('entry_title').text = @config["plot"]["title"]
    @glade.get_widget('entry_yrange_min').text = @config["plot"]["yrange"]["min"].to_s
    @glade.get_widget('entry_yrange_max').text = @config["plot"]["yrange"]["max"].to_s
    @glade.get_widget('entry_kosher_star').text = @config["input"]["kosher_star"].to_s
  end
  
  def get_formatted_filename(filename)
    tmp = filename.split("/")
    tmp2 = Array.new
    flag = false
    tmp.each {|i|
      if (flag)
        tmp2 = tmp2 + [i]
      end
      if (i == "gaostars")
        flag = true
      end
    }
    tmp2.join("/")
  end
  
  def on_button_submit_clicked(widget)
    @glade.get_widget('progressbar').fraction = 0.0
    @config["input"]["path"]   = get_formatted_filename(@glade.get_widget('filebutton_input_dir').filename)
    @config["output"]["path"]  = get_formatted_filename(@glade.get_widget('filebutton_output_dir').filename)
    @config["input"]["lpoint"] = get_formatted_filename(@glade.get_widget('filebutton_eph').filename)
    @config["plot"]["title"]   = @glade.get_widget('entry_title').text
    @config["plot"]["curve"]   = @glade.get_widget('entry_curve').text
    @config["plot"]["yrange"]["min"] = @glade.get_widget('entry_yrange_min').text
    @config["plot"]["yrange"]["max"] = @glade.get_widget('entry_yrange_max').text
    @config["input"]["kosher_star"]  = @glade.get_widget('entry_kosher_star').text
    @config['input']['normalize']     = @glade.get_widget('checkbutton_normalize').active?
    @config["input"]["lpoint_date"]   = @glade.get_widget('entry_lpoint_date').text
    File.open(@config_file, 'w+') { |out| YAML::dump(@config, out) }
    `cd ..; ruby ruby/graph.rb` 
    #`cd ../php; php stars.php`
    @glade.get_widget('progressbar').fraction = 1.0
  end  
  
  def on_menu_help_about_activate(widget)
    @glade.get_widget('aboutdialog').show
  end
  
  def gtk_widget_hide(widget)
    widget.hide
  end

  def gtk_widget_response(widget,signal)
    if (signal == -6)
      @glade.get_widget('aboutdialog').hide
    end
    if (signal == -4)
      @glade.get_widget('aboutdialog').hide
    end
  end
  
  def on_menu_actions_delete_activate_item(widget)
  end
  
  def on_menu_actions_delete_activate(widget)
    `cd ../batch; ruby delete.rb`
  end
  
end

# Main program
if __FILE__ == $0
  # Set values as your own application. 
  PROG_PATH = "gaostars.glade"
  PROG_NAME = "YOUR_APPLICATION_NAME"
  GaostarsGlade.new(PROG_PATH, nil, PROG_NAME)
  Gtk.main
end
